/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.composites;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;
import org.eclipse.uml2.uml.ObjectNode;
import org.eclipse.uml2.uml.Pin;

/**
 * Composite for isControl field of Pins
 *
 * @author Gilles Cannenterre
 * @since 11 avr. 07
 */
public class IsControlTypeComposite implements ISectionComposite {

	/** The element. */
	private ObjectNode element;
	
	/** The is control listener. */
	protected SelectionListener isControlTypeListener;

	/**
	 * Sets the element.
	 * 
	 * @param element the element
	 */
	public void setElement(ObjectNode element) {
		this.element = element;
	}
	
	/** The is control. */
	private Button isControlType = null;

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.composites.ISectionComposite#createContent(org.eclipse.swt.widgets.Composite, org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory)
	 */
	public Composite createContent(Composite parent, TabbedPropertySheetWidgetFactory factory) {
		Composite composite = factory.createFlatFormComposite(parent);
		FormData data;

		isControlType = factory.createButton(composite, "", SWT.CHECK);
		data = new FormData();
		data.left = new FormAttachment(0, 85);
		data.top = new FormAttachment(0, 0);
		isControlType.setLayoutData(data);

		isControlType.addSelectionListener(isControlTypeListener = new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				element.setIsControlType(isControlType.getSelection());
				if (element instanceof Pin) {
					((Pin) element).setIsControl(isControlType.getSelection());					
				}
			}

			public void widgetDefaultSelected(SelectionEvent e){
			}
		});

		CLabel abstractionLabel = factory.createCLabel(composite, "ControlType:"); //$NON-NLS-1$
		data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(isControlType,
				-ITabbedPropertyConstants.HSPACE);
		data.top = new FormAttachment(isControlType, 0, SWT.CENTER);
		abstractionLabel.setLayoutData(data);

		return composite;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.composites.ISectionComposite#refresh()
	 */
	public void refresh() {
		if (!isControlType.isDisposed()){
			if(element instanceof ObjectNode) {
				isControlType.setSelection(element.isControlType());
			}
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.composites.ISectionComposite#dispose()
	 */
	public void dispose(){
		if(isControlType!=null && !isControlType.isDisposed())
			isControlType.removeSelectionListener(isControlTypeListener);
	}
}